package com.sherlockHolmes.init.base.email;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class Email {
    public MailAccount Email() {
        MailAccount account = new MailAccount();
        account.setHost("smtp.163.com");
        account.setPort(25);
        account.setAuth(true);
        account.setFrom("gy18756325174@163.com");
        account.setUser("gy18756325174");
        account.setPass("JACLYTUNMDUFMFUJ");
        return account;
    }
    @Async
    public String sendEmail(String to, String title, String content,boolean isHtml) {
        try {
            MailUtil.send(Email(), CollUtil.newArrayList(to), title, content, isHtml);
            return "邮件发送成功";
        }catch (Exception e) {
            log.error("邮件发送失败", e);
            return "邮件发送失败";
        }
    }
    @Async
    public String sendAllEmail(List<String> to, String title, String content, boolean isHtml) {
        try {
            MailUtil.send(Email(), to, title, content, isHtml);
            return "邮件发送成功";
        }catch (Exception e){
            log.error("邮件发送失败", e);
            return "邮件发送失败";
        }
    }

}
